<?php

/**
 * Contains a function to prepare a source string for display on the web while
 * preserving HTML characters and newline breaks.
 *
 * PHP Version 5
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */

/**
 * A function to prepare a source string for display on the web while 
 * preserving HTML characters and newline breaks.
 *
 * @param string $source The source string to encode.
 *
 * @access public
 * @return string        The encoded string.
 */
function escapeRaw($source)
{
    /**
     * Encodes to the UTF-8 standard.
     *
     * Runs htmlentities to encode everything, including quotes, to the UTF-8
     * charset, and does not re-encode encoded values - to prevent &amp; from
     * becoming &amp;amp;
     */
    $source = htmlentities($source, ENT_QUOTES, 'UTF-8', false);
    
    /**
     * Encodes to the UNICODE standard.
     *
     * Strips out any remaining characters that are not allowed under the
     * UNICODE Consortium rules - ASCII 0 to 31 inclusive and 127+ inclusive by
     * matching and removing any character other than 32 to 126 inclusive, but
     * skipping newline characters (10).
     */
    $source = preg_replace('/[^\x20-\x7e]^\xa/', '', $source);
    
    /** Rolls back HTML tag encoding. */
    $source = str_ireplace('&gt;', '>', $source);
    $source = str_ireplace('&lt;', '<', $source);
    
    /** Rolls back quote encoding. */
    $source = str_ireplace('&quot;', '"', $source);
    $source = str_ireplace('&#039;', '\'', $source);
    
    /** Returns the HTML-encoded string. */
    return $source; 
}

?>